package com.taptech.doufu.ui.view.userdetail

import android.content.Context
import android.graphics.Color
import androidx.core.content.ContextCompat
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONObject
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.gson.Gson
import com.taptech.doufu.R
import com.taptech.doufu.model.user.ListItem
import com.taptech.doufu.model.user.NovelListBean
import com.taptech.doufu.services.personalcenter.AccountService
import com.taptech.doufu.ui.adapter.UserDetailLatestReadAdapter
import com.taptech.doufu.ui.adapter.UserDetailNovelListAdapter
import com.taptech.doufu.util.GsonUtil
import com.taptech.doufu.weex.http.TFHttpError
import com.taptech.doufu.weex.http.TFHttpResponse
import com.taptech.doufu.weex.http.TFHttpUtil
import kotlinx.android.synthetic.main.dialog_set_public_read_record.view.*
import kotlinx.android.synthetic.main.view_user_detail_latest_read.view.*

class UserDetailLatestReadView(context: Context):LinearLayout(context) {

    var readRecordStatus:Int = 0

    init {
        initView()
    }

    private fun initView() {
        val view = LayoutInflater.from(context).inflate(R.layout.view_user_detail_latest_read,null)
        addView(view)
    }

    fun setContentNullData(uid:Int,readRecordStatus:Int) {
        this.readRecordStatus = readRecordStatus
        tvContentNull.visibility = View.VISIBLE
        initSetUi(uid)
    }

    fun setData(uid:Int,readRecordStatus: Int,data: ListItem) {
        try {
            this.readRecordStatus = readRecordStatus
            val info:NovelListBean = GsonUtil.parseJsonWithGson(Gson().toJson(data.data), NovelListBean::class.java)

            if (info.list.size > 0) {
                mRecyclerView.visibility = View.VISIBLE
                layNone.visibility = View.GONE
                val adapter = UserDetailLatestReadAdapter(context)
                adapter.setData(info.list)
                val manager = object : LinearLayoutManager(context, HORIZONTAL,false){
                    override fun canScrollHorizontally(): Boolean {
                        return true
                    }
                }
                mRecyclerView.layoutManager = manager
                mRecyclerView.adapter = adapter
            }else{
                mRecyclerView.visibility = View.GONE
                layNone.visibility = View.VISIBLE
            }
            initSetUi(uid)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    private fun initSetUi(uid:Int) {
        if (AccountService.getInstance().isLogin && AccountService.getInstance().userUid == "$uid") {
            tvSet.visibility = View.VISIBLE
        }else{
            tvSet.visibility = View.GONE
        }
        tvSet.setOnClickListener {
            toastDialog()
        }
    }

    private var tvShow:TextView? = null
    private var tvHide:TextView? = null

    private fun toastDialog() {
        val bottomSheetDialog = BottomSheetDialog(context)
        bottomSheetDialog.setContentView(R.layout.dialog_set_public_read_record)
        val layCancel = bottomSheetDialog.findViewById<View>(R.id.layCancel)
        val layShow = bottomSheetDialog.findViewById<View>(R.id.layShow)
        val layHide = bottomSheetDialog.findViewById<View>(R.id.layHide)
        tvShow = bottomSheetDialog.findViewById(R.id.tvShow)
        tvHide = bottomSheetDialog.findViewById(R.id.tvHide)
        setTextColor()
        layCancel?.setOnClickListener { bottomSheetDialog.dismiss() }
        layShow?.setOnClickListener {
            setReadRecord(true)
            bottomSheetDialog.dismiss()
        }
        layHide?.setOnClickListener {
            setReadRecord(false)
            bottomSheetDialog.dismiss()
        }
        bottomSheetDialog.show()
    }

    private fun setTextColor() {
        if (readRecordStatus == 1) {
            tvShow?.setTextColor(ContextCompat.getColor(context,R.color.text_color_57))
            tvHide?.setTextColor(Color.parseColor("#666666"))
        }else{
            tvShow?.setTextColor(Color.parseColor("#666666"))
            tvHide?.setTextColor(ContextCompat.getColor(context,R.color.text_color_57))
        }
    }


    private fun setReadRecord(isShow: Boolean) {
        val map = HashMap<String, Any>()
        map["switch_type"] = 1
        if (isShow) {
            map["unset"] = 1
        }else{
            map["unset"] = 0
        }
        TFHttpUtil.requestPost("http://api.doufu.la/user/switch",map) { json, error ->
            if (json!=null && json.containsKey("data")){
                val data = json.getJSONObject("data")
                if (data!=null && data.containsKey("affected") && data.getBoolean("affected")){
                    readRecordStatus = if (isShow) {
                        1
                    }else{
                        0
                    }
                    setTextColor()
                    Toast.makeText(context,"设置成功",Toast.LENGTH_LONG).show()
                    return@requestPost
                }
            }
            Toast.makeText(context,"设置失败，请重试",Toast.LENGTH_LONG).show()
        }
    }

}
